package com.hjq.demo.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;
import okhttp3.Call;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.hjq.bar.TitleBar;
import com.hjq.base.BaseDialog;
import com.hjq.demo.R;
import com.hjq.demo.aop.CheckNet;
import com.hjq.demo.aop.Log;
import com.hjq.demo.aop.SingleClick;
import com.hjq.demo.app.AppActivity;
import com.hjq.demo.http.api.AddShipmentApi;
import com.hjq.demo.http.api.AddUserApi;
import com.hjq.demo.http.api.DeleteUserApi;
import com.hjq.demo.http.api.LoginApi;
import com.hjq.demo.http.model.HttpData;
import com.hjq.demo.http.model.RequestHandler;
import com.hjq.demo.manager.InputTextManager;
import com.hjq.demo.other.NewConfig;
import com.hjq.demo.ui.dialog.AddressDialog;
import com.hjq.demo.ui.dialog.MessageDialog;
import com.hjq.demo.ui.fragment.MineFragment;
import com.hjq.http.EasyConfig;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.hjq.widget.view.RegexEditText;

//新增客户信息
public class AddCustomerInformationActivity extends AppActivity {
    private AppCompatEditText aetPleaseEnterCustomerNameInputHint;
    private RegexEditText etPasswordForgetPhone;
    private SettingBar mAddressView;
    /**
     * 省
     */
    private String mProvince = "河南省";
    /**
     * 市
     */
    private String mCity = "开封市";
    /**
     * 区
     */
    private String mArea = "尉氏县";
    private AppCompatButton btnCustomerCommit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_customer_information;
    }
//详细地址
    private RegexEditText rebDetailedAddress;
    //看是新增还是 编辑 默认新增
    private String isType = "2";
    private TitleBar tbTitle;
    private String name = "name";
    private String phoneNumber = "phoneNumber";
    private String address = "address";
    private int id = 0;

    @CheckNet
    @Log
    public static void start(Context context, String type,String name,String phoneNumber,String address,int id) {
        if (TextUtils.isEmpty(type)) {
            return;
        }
        Intent intent = new Intent(context, AddCustomerInformationActivity.class);
        intent.putExtra(NewConfig.INTENT_JUMP_TYPE, type);
        if (type.equals("4")){
            intent.putExtra("name", name);
            intent.putExtra("phoneNumber", phoneNumber);
            intent.putExtra("address", address);
            intent.putExtra("id", id);
        }
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        aetPleaseEnterCustomerNameInputHint = (AppCompatEditText) findViewById(R.id.aet_please_enter_customer_name_input_hint);
        etPasswordForgetPhone = (RegexEditText) findViewById(R.id.et_password_forget_phone);
        mAddressView = (SettingBar) findViewById(R.id.sb_person_data_address);
        btnCustomerCommit = (AppCompatButton) findViewById(R.id.btn_customer_commit);


        rebDetailedAddress = (RegexEditText) findViewById(R.id.reb_detailed_address);

        tbTitle = (TitleBar) findViewById(R.id.tb_title);

        setOnClickListener(mAddressView, btnCustomerCommit);
        InputTextManager.with(this)
                .addView(etPasswordForgetPhone)
                .addView(aetPleaseEnterCustomerNameInputHint)
                .addView(rebDetailedAddress)
                .setMain(btnCustomerCommit)
                .build();
    }


    @Override
    public void onClick(View view) {
        if (view == mAddressView) {
            new AddressDialog.Builder(this)
                    //.setTitle("选择地区")
                    // 设置默认省份
                    .setProvince(mProvince)
                    // 设置默认城市（必须要先设置默认省份）
                    .setCity(mCity)
                    // 不选择县级区域
                    //.setIgnoreArea()
                    .setListener((dialog, province, city, area) -> {
                        String address = province + city + area;
                        if (!mAddressView.getRightText().equals(address)) {
                            mProvince = province;
                            mCity = city;
                            mArea = area;
                            mAddressView.setRightText(address);
                        }
                    })
                    .show();
        } else if (view == btnCustomerCommit) {

            if (etPasswordForgetPhone.getText().toString().length() != 11) {
                etPasswordForgetPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                toast(R.string.common_phone_input_error);
                return;
            }
            if (isType.equals("4")) {
                EasyHttp.put(this)
                        .api(new AddUserApi()
                                .setId(id)
                                .setUsername(aetPleaseEnterCustomerNameInputHint.getText().toString())
                                .setAddress(mAddressView.getRightText()+rebDetailedAddress.getText().toString() )
                                .setPhoneNumber(etPasswordForgetPhone.getText().toString()))
                        .request(new HttpCallback<HttpData<AddUserApi.Bean>>(this) {

                            @Override
                            public void onSucceed(HttpData<AddUserApi.Bean> data) {
                                toast(R.string.common_edit_complete);
                                finish();
                            }
                            @Override
                            public void onFail(Exception e) {
                                super.onFail(e);
                                toast(R.string.common_edit_error);
                            }
                        });
                return;
            } else if (isType.equals("2")) {
                EasyHttp.post(this)
                        .api(new AddUserApi()
                                .setUsername(aetPleaseEnterCustomerNameInputHint.getText().toString())
                                .setAddress(mAddressView.getRightText()+rebDetailedAddress.getText().toString() )
                                .setPhoneNumber(etPasswordForgetPhone.getText().toString()))
                        .request(new HttpCallback<HttpData<AddUserApi.Bean>>(this) {

                            @Override
                            public void onSucceed(HttpData<AddUserApi.Bean> data) {
                                toast(R.string.common_add_complete);
                                finish();
                            }
                            @Override
                            public void onFail(Exception e) {
                                super.onFail(e);
                                toast(R.string.common_add_error);
                            }
                        });
                return;
            }
        }
    }

    @SingleClick
    @Override
    public void onRightClick(View view) {
        // 消息对话框
        new MessageDialog.Builder(getActivity())
                // 标题可以不用填写
                .setTitle("删除")
                // 内容必须要填写
                .setMessage("是否确认删除本客户信息?")
                // 确定按钮文本
                .setConfirm(getString(R.string.common_confirm))
                // 设置 null 表示不显示取消按钮
                .setCancel(getString(R.string.common_cancel))
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setListener(new MessageDialog.OnListener() {

                    @Override
                    public void onConfirm(BaseDialog dialog) {
                        DeleteUser();
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        toast("取消删除");
                    }
                })
                .show();
    }

    private void DeleteUser(){
        EasyHttp.post(this)
                .api(new DeleteUserApi()
                        .setIds(id+"")
                )
                .request(new HttpCallback<HttpData<DeleteUserApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<DeleteUserApi.Bean> data) {
                        //编辑完成
                        toast(R.string.common_edit_complete);
                        finish();
                    }
                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        toast(R.string.common_edit_error);
                    }
                });
    }

    @Override
    protected void initData() {
        isType = getString(NewConfig.INTENT_JUMP_TYPE);
        if (isType.equals("4")) {
            tbTitle.setRightTitle("删除");
            tbTitle.setRightTitleColor(R.color.common_accent_color);
            mAddressView.setVisibility(View.GONE);
            name = getString("name");
            phoneNumber = getString("phoneNumber");
            address = getString("address");
            id = getInt("id");
            aetPleaseEnterCustomerNameInputHint.setText(name);
            etPasswordForgetPhone.setText(phoneNumber);
            rebDetailedAddress.setText(address);
        }
    }

    private void addUserApiHttp(){


    }

}